package com.ews.onlineexchange.controller.assess;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.ServiceSupplier;
import com.ews.onlineexchange.model.assess.AssessRecord;
import com.ews.onlineexchange.model.assess.Dict;
import com.ews.onlineexchange.model.assess.PeriodType;
import com.ews.onlineexchange.service.EntityQuery;
import com.ews.onlineexchange.util.PeriodUtils;

public class AssessReocrdLocator extends EntityQuery<AssessRecord> {

	/**
	 * 
	 */
	private static final long 	serialVersionUID = 1L;

	private ServiceSupplier		supplier;
	private Dict				dict;
	
	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<AssessRecord> entity ) {
		
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		
		restrictions.add( cb.equal( entity.join( "serviceSupplier" ).get( "id" ), getSupplier().getId() ) );
		
		restrictions.add( cb.equal( entity.join( "dict" ).get( "id" ), getDict().getId() ) );
		
		restrictions.add( cb.isFalse( entity.get( "override" ) ) );
				
		PeriodType periodType = getDict().getPeriodType();
		if ( periodType.equals( PeriodType.Year ) ) {
			restrictions.add( cb.equal( entity.get( "periodYear" ), PeriodUtils.getPeriodYear( periodType ) ) );
		} else if ( periodType.equals( PeriodType.Season ) || periodType.equals( PeriodType.Month ) ) {
			restrictions.add( cb.equal( entity.get( "periodYear" ), PeriodUtils.getPeriodYear( periodType ) ) );
			restrictions.add( cb.equal( entity.get( "periodSeqNo" ), PeriodUtils.getPeriodSn( periodType ) ) );
		}
		
		return restrictions;
	}

	public ServiceSupplier getSupplier() {
		return supplier;
	}

	public void setSupplier( ServiceSupplier supplier ) {
		this.supplier = supplier;
	}

	public Dict getDict() {
		return dict;
	}

	public void setDict( Dict dict ) {
		this.dict = dict;
	}

}
